package com.demo.my.Controller;

import com.demo.my.Entity.Employee;
import com.demo.my.Service.EmployeeService;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Date;

@WebServlet("/UpEmployee")
public class UpdateEmployeeServlet extends HttpServlet {
    private EmployeeService employeeService=new EmployeeService();
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        try {
            Integer employeeNumber = Integer.parseInt(req.getParameter("employeeNumber"));
            String name = req.getParameter("name");
            String password = req.getParameter("password");
            String gender = req.getParameter("gender");
            String date = req.getParameter("date");
            SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
            Date birthday = format.parse(date);
            System.out.println(birthday);
            String telephone = req.getParameter("telephone");
            String email = req.getParameter("email");
            String address = req.getParameter("address");
            String education = req.getParameter("education");
            Integer departmentNumber = Integer.parseInt(req.getParameter("departmentNumber"));
            Integer positionNumber = Integer.parseInt(req.getParameter("positionNumber"));
            String notes = req.getParameter("notes");
            Employee employee = new Employee(1, employeeNumber, name, gender, birthday, telephone, email, address, "null", education, departmentNumber, positionNumber, new Date(), password, notes, null, null);
            employeeService.updateEmployee(employee);
            req.getRequestDispatcher("employeeManagement").forward(req,resp);
        } catch (Exception throwables) {
            throwables.printStackTrace();
        }
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        doGet(req, resp);
    }
}
